<template>
    <div class="community_nav" ref="navbar">
        <span @click="navigateTo('index')">首页</span>
        <span @click="navigateTo('community_specialcolumn')">专栏</span>
        <span @click="navigateTo('life')">我的</span>
        <span @click="navigateTo('community_delivery')">创作</span>
    </div>
    <div class="community">
        <div id="search">
            <input type="text" v-model="search_value" @focus="search_onfocus" @focusout="search_outfocus" :placeholder="inputdata" />
            <div style="display: inline-block; width: 80px; text-align: center;" @click="search_click">搜索</div>
        </div>
       <div class="search_his_pre" v-show="showSearchHistory">
            <div>
                <p>搜索历史:</p>
                <div>
                    <span  v-for="(i,index) in index_user_search_history" :key="index" @mouseover="showTooltip($event,i)"  @mouseleave="hideTooltip"  >
                        {{i}}
                    </span> 
                </div>
            </div>

            <i class="divider"></i>
            <div>
                <p>猜你想搜:</p>
                <div>
                  <span v-for="(i,index) in index_user_search_guess" :key="index" @mouseover="showTooltip($event,i)"  @mouseleave="hideTooltip" >
                        {{i}}
                  </span>
                </div>
            </div>
        </div>
        <div id="preview_list">
            <span v-for="(i, index) in index_rec_a" :key="index">
                <span @click="click_rec_span(i)">{{ i }}</span>
            </span>
        </div>
        <div v-if="index_text_Carousel_all.length!== 0">
            <p>今日推荐</p>
            <div>
                <div v-if="index_image_Carousel.length !== 0 && zixundataAll.length !== 0" class="recommendation-container">
                    <div>
                        <a-carousel style="width: 500px; height: 200px;" show-arrow="never" direction="horizontal" indicator-position="top" indicator-type="slider" auto-play="true" @change="index_img__Carousel_change">
                            <a-carousel-item v-for="image in index_image_Carousel" :key="image">
                                <img :src="image" :style="{ width: '100%' }" />
                            </a-carousel-item>
                        </a-carousel>
                        <div>
                            <Ellipsis :title="index_text_Carousel_current" @click="index_text_Carousel_click" :text="index_text_Carousel_current" style="font-size: 16px; padding: 10px;" :length="120" theme="light" />
                        </div>
                    </div>
                    <div>
                        <INDEX_REC_INFO :items="zixundataAll" />
                    </div>
                </div>
                <div v-else-if="index_image_Carousel.length !== 0" class="centered">
                    <a-carousel style="width: 500px; height: 200px;" show-arrow="never" direction="horizontal" indicator-position="top" indicator-type="slider" auto-play="true" @change="index_img__Carousel_change">
                        <a-carousel-item v-for="image in index_image_Carousel" :key="image">
                            <img :src="image" :style="{ width: '100%' }" />
                        </a-carousel-item>
                    </a-carousel>
                    <div>
                        <Ellipsis :title="index_text_Carousel_current" @click="index_text_Carousel_click" :text="index_text_Carousel_current" style="font-size: 16px; padding: 10px;" :length="120" theme="light" />
                    </div>
                </div>
                <div v-else-if="zixundataAll.length !== 0" class="centered">
                    <INDEX_REC_INFO :items="zixundataAll" />
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref ,inject } from 'vue';
import {
    get_index_recommend, search_article, get_index_recommend_span,
    get_user_search_history, get_index_search_guess
} from "../../api/community/main.js";
import { useRouter } from 'vue-router';
import INDEX_REC_INFO from "./index_rec_zixu.vue";

let tooltip = null;
let zixundataAll = ref([]);
let index_user_search_history = ref([1, 2, 3]);
let index_user_search_guess = ref([1, 2, 3]);
let user = inject('user')();
let show_global_button = inject('global_button');
let inputdata = ref("在这里搜索");
let showSearchHistory = ref(false);
let search_value = ref();
let Router = useRouter();
let community_store = inject('community')();
let index_rec_a = ref([]);
let index_text_Carousel_current = ref();
let index_text_Carousel_all = ref([]);
const index_image_Carousel = ref([]);
const index_img__Carousel_change = function (index) {
    index_text_Carousel_current.value = index_text_Carousel_all.value[index - 1];
};

const index_text_Carousel_click = function (e) {
    const title = e.target.parentNode.getAttribute("title");
    let data = community_store.value.get_index_Carousel_info_Bytitle(title);
    const nav_to = Router.resolve({ name: 'community_render_index_rec_text', query: { id: data.id } });
    window.open(nav_to.href, '_blank');
};

const search_click = async function () {
    if (search_value.value) {
        community_store.value.set_searchQuery(search_value.value);
        await new Promise(resolve => setTimeout(resolve, 50));
       
        const nav_to = Router.resolve({
            name: "community_render_search_text"
        });
        console.log(community_store.value.get_searchQuery())
        window.open(nav_to.href,'_blank');
    }
};

const search_onfocus = async function () {
    if (user.value.get_user()) { 
        const data = await get_user_search_history(user.value.get_user().id);
        index_user_search_history.value = data || [];
    }
    index_user_search_guess.value = await get_index_search_guess(user.value.get_user().id);
    showSearchHistory.value = true;
};

const search_outfocus = function () {
    hideTooltip();
    showSearchHistory.value = false;
};

const click_rec_span = function (index) {
    search_value.value = index;
    search_click();
};

const navigateTo = function (section) {
    if (show_global_button.value) {
        Router.push({ name: `${section}` });
    } else {
        const route = Router.resolve({ name: `${section}` });
        window.open(route.href, '_blank');
    }
};

const showTooltip = function (event, text) {
    if (tooltip) hideTooltip();
    tooltip = document.createElement("span");
    tooltip.className = "tooltip-span";
    tooltip.textContent = text;
    search_value.value = text;

    Object.assign(tooltip.style, {
        position: "absolute",
        background: "white",
        color: "#19be6b",
        padding: "4px 8px",
        borderRadius: "4px",
        fontSize: "12px",
        pointerEvents: "auto",
        zIndex: "0",
        whiteSpace: "nowrap",
        boxShadow: "0px 2px 4px rgba(0, 0, 0, 0.2)",
    });
    document.body.appendChild(tooltip);
    updateTooltipPosition(event);
};

const updateTooltipPosition = (event) => {
    if (tooltip) {
        tooltip.style.left = `${event.pageX + 10}px`;
        tooltip.style.top = `${event.pageY + 10}px`;
    }
};

const hideTooltip = () => {
    if (tooltip) {
        document.body.removeChild(tooltip);
        tooltip = null;
    }
};

const handleScroll = () => {
    try {
        const navbar = document.getElementById("navbar");
        const stickyPoint = window.innerHeight * 2 / 3;
        if (window.pageYOffset >= stickyPoint) {
            navbar.classList.add("sticky");
        } else {
            navbar.classList.remove("sticky");
        }
    } catch (error) {}
};

onMounted(async () => {
    index_rec_a.value = await get_index_recommend_span();
    window.addEventListener('scroll', handleScroll);

    let pre_Data;
    if (community_store.value.get_index_Carousel_info_ALL().length === 0 || !community_store.value.get_index_Carousel_info_ALL()) {
        pre_Data = await get_index_recommend(0);
        zixundataAll.value = pre_Data.slice(3, pre_Data.length);
        community_store.value.set_index_Carousel_info_All(pre_Data);

        if (pre_Data && pre_Data !== 404 && pre_Data.length !== 0) {
            let title = [];
            try {
                index_image_Carousel.value.push(pre_Data[0].images[0]);
                index_image_Carousel.value.push(pre_Data[1].images[0]);
                index_image_Carousel.value.push(pre_Data[2].images[0]);
            } catch (error) {}

            pre_Data.forEach(element => {
                title.push(element.title);
            });
            index_text_Carousel_current.value = title[0];
            index_text_Carousel_all.value = title;
        }
    } else {
        pre_Data = community_store.value.get_index_Carousel_info_ALL();
        let title = [];
        zixundataAll.value = pre_Data.slice(3, pre_Data.length);
        try {
            index_image_Carousel.value.push(pre_Data[0].images[0]);
            index_image_Carousel.value.push(pre_Data[1].images[0]);
            index_image_Carousel.value.push(pre_Data[2].images[0]);
        } catch (error) {}

        pre_Data.forEach(element => {
            title.push(element.title);
        });
        index_text_Carousel_current.value = title[0];
        index_text_Carousel_all.value = title;
    }
});
</script>

<style scoped>


.community_nav {
    color: white; /* 文字颜色为白色 */
    font-size: 16px;
    padding: 15px 0; /* 上下留白 */
    text-align: center;
    position: relative;
    width: 100%;
    background: linear-gradient(135deg, #ac85d6, #6397f1); /* 渐变色背景 */
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 阴影效果 */
    border-radius: 0 0 20px 20px; /* 底部圆角 */
    transition: all 0.3s ease; /* 平滑过渡 */
}

.community_nav.sticky {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 100;
    border-radius: 0; /* 固定时取消圆角 */
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2); /* 固定时阴影加深 */
}

.community_nav > span {
    font-size: 18px;
    margin: 0 20px; /* 增加间距 */
    cursor: pointer;
    color: rgba(255, 255, 255, 0.8); /* 半透明文字 */
    padding: 10px 15px; /* 增加内边距 */
    border-radius: 25px; /* 圆角 */
    transition: all 0.3s ease; /* 平滑过渡 */
}

.community_nav > span:hover {
    color: white; /* 悬停时文字颜色变为纯白 */
    background-color: rgba(255, 255, 255, 0.1); /* 悬停时背景微亮 */
    transform: scale(1.05); /* 悬停时稍微放大 */
}

.community_nav > span:active {
    transform: scale(0.95); /* 点击时稍微缩小 */
}

.community {
    padding: 50px 0 0 0;
    user-select: none;
    position: relative;
    /* background-color: #f5f5f5; */
}

.community > div {
    display: flex;
    justify-content: center;
}

.community > div[id="search"] > input[type="text"] {
    margin-top: 100px;
    width: 800px;
    height: 40px;
    border: 1px solid #ccc;
    border-radius: 5px 0 0 5px;
    padding: 0 10px;
    font-size: 16px;
    outline: none;
    box-sizing: border-box;
    margin-bottom: 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: border-color 0.3s ease;
}

.community > div[id="search"] > input[type="text"]:focus {
    border-color: #50e3c2;
}

.community > div[id="search"] > div {
    display: inline-block;
    padding: 0 10px;
    margin-top: 100px;
    height: 40px;
    width: 50px;
    line-height: 40px;
    color: #fff;
    background-color: #50e3c2;
    border: 1px solid #50e3c2;
    border-radius: 0 5px 5px 0;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

.community > div[id="search"] > div:hover {
    background-color: #3bb99e;
}

/* 推荐列表 */
#preview_list {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    margin: 20px auto;
    width: 90%;
    max-width: 800px;
}
#preview_list span {
    display: inline-block;
    padding: 4px 8px;
    font-size: 15px;
    color: #808695;
    background-color: #f1f1f1;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

#preview_list>span:hover {
    background-color: #50e3c2;
    color: #fff;
}
#preview_list>span>span:hover {
    background-color: #7ff7bb;
    color: #fff;
}

.search_his_pre {

    width: 860px;
    max-height: 200px;
    margin: 0 auto;
    margin-top: -18px;
    margin-bottom: 5px;
    padding: 5px;
    background-color: #f9fafa;
    align-items: center;
    font-size: 16px;
    border-radius: 8px;
}

div.community>div.search_his_pre > div {
    width: 50%;
    height: 180px;
    display: block;
    padding: 10px;
    overflow: scroll;
}

.search_his_pre > div::-webkit-scrollbar {
    display: none;
}



.divider {
    border: 1px solid #808695;
    height: 200px;
}


.search_his_pre > div > div {
    width: 100%;
    padding: 10px;
    font-size: 14px;
    display: flex; 
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

/* 新增用于控制推荐处于中间位置 */
.centered {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    text-align: center;
    margin: 0 auto;
}

.recommendation-container {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 20px;
}

::v-deep(.search_his_pre > div > div > span) {
    flex-shrink: 0;
    display: inline-block;
    height: 30px;
    min-width: 50px; 
    max-width: 150px;
    padding: 4px 8px;
    background-color: rgba(80, 227, 194, 0.1);
    border: 1px solid #50e3c2;
    color: #50e3c2;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
    white-space: nowrap; 
    overflow: hidden;
    text-overflow: ellipsis; 
}



div > div:nth-child(4) {
    display: block;
    width: 1500px;
    margin: 0 auto;
    text-align:center;
}

div> div:nth-child(4) > div:nth-child(2){
    text-align: left;
}

div > div:nth-child(4) > div {
    margin-top: 20px;
    display: inline-block;
    vertical-align: top;
}

div > div:nth-child(4) > div:nth-child(3) {
    width: 780px;
    height: 380px;
    margin-left: 10px;
    margin-right: 10px;
    text-align: left;
}
div > div:nth-child(4) > p {
    font-size: 20px;
    margin-top: 20px;
    text-align: center;
    color: #8b8686;
}


/* 移动端适配 */
@media screen and (max-width: 768px) {
    .community_nav {
        font-size: 14px;
        padding: 10px 0;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    .community_nav > span {
        font-size: 12px;
        margin: 0 10px;
        padding: 8px 10px;
    }

    .community > div[id="search"] > input[type="text"] {
        width: 90%;
        margin-top: 20px;
        margin-bottom: 10px;
    }

    .community > div[id="search"] > div {
        width: 20%;
        margin-top: 20px;
    }

    .search_his_pre {
        width: 95%;
        height: auto;
        margin-top: 10px;
    }

    #preview_list {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
    }

    #preview_list > span {
        width: 80px;
        font-size: 12px;
    }

    a-carousel {
        width: 100%;
        height: 180px;
    }
}
</style>